package com.juku2024.juku.adapter

import com.chad.library.adapter.base.BaseDelegateMultiAdapter
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.juku2024.juku.R
import com.juku2024.juku.model.FeedbackInfo
import com.juku2024.juku.tools.TimeUtils
import com.juku2024.juku.widget.HttpTextView

class FeedbackAdapter(feedbackInfos: ArrayList<FeedbackInfo>) :
    BaseDelegateMultiAdapter<FeedbackInfo, BaseViewHolder>(feedbackInfos) {

    companion object {
        const val TYPE_ME = 1
        const val TYPE_SYS = 2
        const val FEEDBACK_EMPTY = 3
    }

    init {
        setMultiTypeDelegate(object : BaseMultiTypeDelegate<FeedbackInfo>() {
            override fun getItemType(data: List<FeedbackInfo>, position: Int): Int {
                return data[position].roleId
            }
        })

        getMultiTypeDelegate()?.apply {
            addItemType(
                TYPE_ME,
                R.layout.item_feedback_me
            )
            addItemType(
                TYPE_SYS,
                R.layout.item_feedback_sys
            )
            addItemType(
                FEEDBACK_EMPTY,
                R.layout.item_feedback_empty
            )
        }
    }

    override fun convert(holder: BaseViewHolder, item: FeedbackInfo) {
        val actionDate = item.actionDate?:0L
        if (item.roleId == TYPE_ME) {
            holder.setText(R.id.user_content_tv, item.msg)
                .setText(R.id.user_time_tv, TimeUtils.feedbackTime(actionDate * 1000))
        } else if (item.roleId == TYPE_SYS) {
            val httpTextUtils = holder.getViewOrNull<HttpTextView>(R.id.user_content1_tv)
            httpTextUtils?.setOpenRegionUrl(true)
            httpTextUtils?.setUrlText(item.msg)
            holder.setText(R.id.sys_time_tv, TimeUtils.feedbackTime(actionDate * 1000))
        }
    }
}